var api = require('../../../config/api.js');
var util = require('../../../utils/util.js');
var user = require('../../../utils/user.js');
var check = require('../../../utils/check.js');
var app = getApp();
Page({
  data: {
    username: '',
    password: '',
    code: '',
    password: '',
    confirmPassword: '',
    mobile: '',
    loginErrorCount: 0,
    isShow: true, //是否显示登录页
    codeTip: 0
  },
  //切换登录或注册页
  tabPage:function(e){
    let sign = e.currentTarget.dataset.sign;
    console.log('sing:'+sign)
    let flag = true
    if(sign === 'login'){
      flag = true
    }else{
      flag = false
    }
    console.log('isShow:'+this.data.isShow)
    this.setData({
      isShow: flag
    })
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    // 页面渲染完成

  },
  onReady: function () {

  },
  onShow: function () {
    // 页面显示
  },
  onHide: function () {
    // 页面隐藏

  },
  onUnload: function () {
    // 页面关闭

  },
  //发送验证码
  sendCode: function () {
    let that = this;
    let  val = null
    if (that.data.codeTip != 0){
      console.log('不求人')
      return 
    }
    if (that.data.mobile == ''){
      util.showErrorToast('手机号为空！')
    }
    if (!check.isValidPhone(this.data.mobile)) {
      util.showErrorToast('手机号格式不对！')
      return false;
    }
    wx.request({
      url: api.AuthRegisterCaptcha,
      data: {
        mobile: that.data.mobile
      },
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        if (res.data.errno == 0) {
          wx.showModal({
            title: '发送成功',
            content: '验证码已发送',
            showCancel: false
          });
          that.setData({
            codeTip: 60
          })
          util.timer(that)
        }
        else {
          wx.showModal({
            title: '错误信息',
            content: res.data.errmsg,
            showCancel: false
          });
        }
      }
    });
  },
  accountLogin: function () {
    var that = this;
    util.getCurrentPagePath();
    if (this.data.password.length < 1 || this.data.username.length < 1) {
      wx.showModal({
        title: '错误信息',
        content: '请输入用户名和密码',
        showCancel: false
      });
      return false;
    }

    wx.request({
      url: api.AuthLoginByAccount,
      data: {
        username: that.data.username,
        password: that.data.password
      },
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        if (res.data.errno == 0){
          that.setData({
            loginErrorCount: 0
          });
          app.globalData.hasLogin = true;
          wx.setStorageSync('userInfo', res.data.data.userInfo);
          console.log("^^^^^^^^^^^^^^^")
          console.log(res.data.data)
          console.log("^^^^^^^^^^^^^^^")
          wx.setStorage({
            key:"token",
            data: res.data.data.token,
            success: function(){
              wx.reLaunch({
                url: '/pages/catalog/catalog',
                success:function(e){
                  var page = getCurrentPages()
                  console.log(page)
                  if (page == undefined || page == null) return;
                  wx.startPullDownRefresh();
                }
              });
            }
          });
        }
        else{
          that.setData({
            loginErrorCount: that.data.loginErrorCount + 1
          });
          app.globalData.hasLogin = false;
          util.showErrorToast('账户登录失败');
        }
      }
    });
  },
  startRegister: function () {
    var that = this;

    if (this.data.password.length < 3 || this.data.username.length < 3) {
      wx.showModal({
        title: '错误信息',
        content: '用户名和密码不得少于3位',
        showCancel: false
      });
      return false;
    }

    if (this.data.password != this.data.confirmPassword) {
      wx.showModal({
        title: '错误信息',
        content: '确认密码不一致',
        showCancel: false
      });
      return false;
    }

    if (this.data.mobile.length == 0 || this.data.code.length == 0) {
      wx.showModal({
        title: '错误信息',
        content: '手机号和验证码不能为空',
        showCancel: false
      });
      return false;
    }

    if (!check.isValidPhone(this.data.mobile)) {
      wx.showModal({
        title: '错误信息',
        content: '手机号输入不正确',
        showCancel: false
      });
      return false;
    }

    wx.request({
      url: api.AuthRegister,
      data: {
        username: that.data.username,
        password: that.data.password,
        mobile: that.data.mobile,
        code: that.data.code
      },
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        if (res.data.errno == 0) {
          app.globalData.hasLogin = true;
          wx.setStorageSync('userInfo', res.data.data.userInfo);
          wx.setStorage({
            key: "token",
            data: res.data.data.token,
            success: function () {
              wx.switchTab({
                url: '/pages/ucenter/index/index'
              });
            }
          });
        }
        else {
          wx.showModal({
            title: '错误信息',
            content: res.data.errmsg,
            showCancel: false
          });
        }
      }
    });
  },
  bindUsernameInput: function (e) {

    this.setData({
      username: e.detail.value
    });
    console.log('username:' + this.data.username)
  },
  bindPasswordInput: function (e) {

    this.setData({
      password: e.detail.value
    });
    console.log('password:' + this.data.password)
  },
  bindConfirmPasswordInput: function (e) {

    this.setData({
      confirmPassword: e.detail.value
    });
    console.log('confirmPassword:' + this.data.code)
  },
  bindMobileInput: function (e) {

    this.setData({
      mobile: e.detail.value
    });
    console.log('mobile:' + this.data.mobile)
  },
  bindCodeInput: function (e) {

    this.setData({
      code: e.detail.value
    });
    console.log('code:'+ this.data.code)
  },
  clearInput: function (e) {
    switch (e.currentTarget.id) {
      case 'clear-username':
        this.setData({
          username: ''
        });
        break;
      case 'clear-password':
        this.setData({
          password: ''
        });
        break;
      case 'clear-confirm-password':
        this.setData({
          confirmPassword: ''
        });
        break;
      case 'clear-mobile':
        this.setData({
          mobile: ''
        });
        break; 
      case 'clear-code':
        this.setData({
          code: ''
        });
        break;
    }
  }
})